//
//  DLAlertView.m
//  delongzhitou
//
//  Created by Mr.Wang on 2019/1/9.
//  Copyright © 2019 delongzhitou. All rights reserved.
//

#import "DLAlertView.h"

@interface DLAlertView ()
@property (nonatomic, strong) UIWindow *osWind;

@property (nonatomic, strong) UIView *shadowView;

@end

@implementation DLAlertView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIWindow* osWind = [[UIApplication sharedApplication] keyWindow];
        self.osWind = osWind;
        
        UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        [self.osWind addSubview:shadowView];
        [self.osWind bringSubviewToFront:shadowView];
        self.shadowView = shadowView;
        shadowView.hidden = YES;
        shadowView.backgroundColor = kColorRGBA(100, 100, 100, 0.7);
        
        self.backgroundColor = [UIColor whiteColor];
        self.center = osWind.center;
        [self.osWind addSubview:self];
        [self resetY:self.y - kAutoWidth(70)];
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shadowViewTap)];
        [shadowView addGestureRecognizer:tapGes];
        
        self.hidden = YES;
    }
    return self;
}
- (void) shadowViewTap {
    [self endEditing:YES];
    [self alertDismiss];
}

- (void) setAlertViewType:(DLAlertViewType)alertViewType {
    _alertViewType = alertViewType;
    switch (alertViewType) {
        case DLAlertViewType_allScreen:
        {
            self.shadowView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        }
            break;
        case DLAlertViewType_withoutNavigation:
        {
            [self.shadowView resetY:kHeightNavigation];
            [self.shadowView resetHeight:kScreenHeight - kHeightNavigation];
            
        }
            break;
        default:
            break;
    }
}

- (void) alertShow {
    self.hidden = NO;
    self.shadowView.hidden = NO;
}
- (void) alertDismiss {
    self.hidden = YES;
    self.shadowView.hidden = YES;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
